import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {ValidateProvider} from "../../providers/validate/validate";
import {ConfigProvider} from "../../providers/config/config";
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {UtilsProvider} from "../../providers/utils/utils";

@IonicPage({
    segment: 'member/wallet/withdraw',
    defaultHistory: ['MoneyPage']
})
@Component({
    selector: 'page-money-ts',
    templateUrl: 'money-ts.html',
})
export class MoneyTsPage {
    postData = {
        amount: '',
        openid: '',
        token: ''
    };
    moneyInfo = {
        money: 0.00
    };

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public validate: ValidateProvider,
        public config: ConfigProvider,
        public httpService: HttpServicesProvider,
        public util: UtilsProvider
    ) {
        if (!this.validate.checkLogin()) {
            this.navCtrl.push('LoginPage');
        }
    }

    //判断用户是否登录
    ionViewCanEnter(){
        return this.validate.checkLogin();
    }

    ionViewWillEnter() {
        let token = this.config.getToken();
        let openid = this.config.getOpenid();
        this.postData = {
            openid: openid,
            amount: '',
            token: token
        };
        this.getData();
    }

    getData() {
        this.httpService.requestData('/echarge/index/token/' + this.config.getToken(), response => {
            this.moneyInfo.money = response['money'] ? response['money'] : 0.00;
        });
    }

    doWithdraw() {
        if(parseFloat(this.postData.amount) <= 0){
            this.util.presentAlert('请输入提现金额');
            return false;
        }
        if (parseFloat(this.postData.amount) > this.moneyInfo.money) {
            this.util.presentAlert('提现金额不能大于当前账户余额');
            return false;
        }
        this.httpService.requestPostData('/echarge/get_cash',this.postData,response=>{
            if(response.code == 200){
                this.util.presentAlert('提现成功！');
                this.navCtrl.popToRoot().then(()=>{
                    this.navCtrl.push('MoneyPage');
                })
            }else{
                this.util.presentAlert(response.message);
            }
        });
    }

}
